<?php

class Ads extends Controller {
	
	function Ads()
	{
		parent::Controller();
		$this->load->model('Product_model');
		$this->load->model('Product_detail_model');
		$this->load->model('Product_image_model');
	}
	
	function index() {
		//check login
		if (!$this->session->userdata('ss_email') || $this->session->userdata('ss_member_type')!=2) {
			redirect('login');
		}	
		
		$ary_where = array('p.member_id'=>$this->session->userdata('ss_member_id'));
		
		$total_rows = $this->Product_model->countProduct($ary_where);
		$per_page = '2';
		$this->pagination->initialize(array(
			'base_url'=>base_url().'ads/index/',
			'total_rows'=>$total_rows,
			'per_page'=>$per_page,
		));
		
		$paging = array(
			'per_page'=>$per_page,
			'limit'   =>($this->uri->segment(3)=='')?'0':$this->uri->segment(3),
		);

		$product_info = $this->Product_model->getProducts($ary_where, $paging);
		
		$data['paging'] = $this->pagination->create_links();
		$data['product_info'] = $product_info;
		$data['inc_script'] = '';
		$this->load->view('ads_view',$data);
	}
	
	function add() {
		//check login
		if (!$this->session->userdata('ss_email') || $this->session->userdata('ss_member_type')!=2) {
			redirect('login');
		}
		
		//load model
		$this->load->model('City_model');
		$this->load->model('District_model');
		$this->load->model('Country_model');
		
		//get value from form
		$product_detail_info = array(
			'title' => $this->input->post('tf_title'),
			'condition' => $this->input->post('tf_condition'),
			'detail_content' => $this->input->post('ta_detail_content'),
		);
		
		$product_info = array(
			'member_id' => $this->session->userdata('ss_member_id'),
			'price' => $this->input->post('tf_price'),
			'price_discount' => $this->input->post('tf_price_discount'),
			'country_id' => $this->input->post('sl_country_id'),
			'city_id' => $this->input->post('sl_city_id'),
			'district_id' => $this->input->post('sl_district_id'),
			'address' => $this->input->post('tf_address'),
			'geo_location' => $this->input->post('tf_geo_location'),
			'phone' => $this->input->post('tf_phone'),
			'date_begin' => datetime_mysql($this->input->post('tf_date_begin')),
			'date_end' => datetime_mysql($this->input->post('tf_date_end')),
			'quantity' => $this->input->post('tf_quantity'),
		);
		
		$product_image_info =  array('ary_uri_path' => $this->input->post('ckb_uri_path'));
		//end get value from form
		
		$form_info = array_merge($product_detail_info, $product_info,$product_image_info);
		
		if ($this->input->post('btn_submit')) {
			//set form validation
			$form_validation = array(
				array('field' => 'tf_title','label' => 'lang:title','rules' => 'required|max_length[45]'),
				array('field' => 'tf_condition','label' => 'lang:condition','rules' => 'required|max_length[45]'),
				array('field' => 'ta_detail_content','label' => 'lang:detail_content','rules' => 'required|max_length[45]'),
				array('field' => 'tf_price','label' => 'lang:price','rules' => 'required|max_length[45]'),
				array('field' => 'tf_price_discount','label' => 'lang:price_discount','rules' => 'required|max_length[45]'),
				array('field' => 'tf_address','label' => 'lang:address','rules' => 'required|max_length[45]'),
				array('field' => 'sl_country_id','label' => 'lang:country','rules' => 'required|max_length[45]'),
				array('field' => 'sl_city_id','label' => 'lang:city','rules' => 'required|max_length[45]'),
				array('field' => 'sl_district_id','label' => 'lang:district','rules' => 'required|max_length[45]'),
				array('field' => 'tf_phone','label' => 'lang:phone','rules' => 'required|max_length[45]'),
				array('field' => 'tf_date_begin','label' => 'lang:date_begin','rules' => 'required|max_length[45]'),
				array('field' => 'tf_date_end','label' => 'lang:date_end','rules' => 'required|max_length[45]'),
				array('field' => 'tf_quantity','label' => 'lang:quantity','rules' => 'required|max_length[45]'),
			);
			$this->form_validation->set_rules($form_validation);
			
			//--check form validation
			if ($this->form_validation->run()==true) {
				//--insert into Product table and return id
				$product_id = $this->Product_model->addProduct($product_info);
				//--insert into Product_image table and return id
				foreach ($product_image_info['ary_uri_path'] as $key => $value) {
					$ary_info = array(
						'product_id' => $product_id,
						'uri_path' => $value,
					);
					$this->Product_image_model->addProduct_image($ary_info);
				}				
				//--insert into Product_detail table and return id
				$product_detail_info['product_id'] = $product_id;
				$this->Product_detail_model->addProduct_detail($product_detail_info);
				redirect('ads/success');	
			}
		}
		
		$data['city_info'] = $this->City_model->getCities();
		$data['district_info'] = $this->District_model->getDistricts();
		$data['country_info'] = $this->Country_model->getCountries();	
		$data['form_info'] = $form_info;
		$data['inc_script'] = '<script type="text/javascript" src="'.base_url().'js/ads.js"></script>';
		$this->load->view('ads_add_view',$data);
	}
	
	function edit() {
		//check login
		if (!$this->session->userdata('ss_email') || $this->session->userdata('ss_member_type')!=2) {
			redirect('login');
		}
		
		//load model
		$this->load->model('City_model');
		$this->load->model('District_model');
		$this->load->model('Country_model');
		
		//get product_id from url
		$id = $this->uri->segment(3);
		
		//get value from form
		$product_detail_info = array(
			'title' => $this->input->post('tf_title'),
			'condition' => $this->input->post('tf_condition'),
			'detail_content' => $this->input->post('ta_detail_content'),
		);
		
		$product_info = array(
			'member_id' => $this->session->userdata('ss_member_id'),
			'price' => $this->input->post('tf_price'),
			'price_discount' => $this->input->post('tf_price_discount'),
			'country_id' => $this->input->post('sl_country_id'),
			'city_id' => $this->input->post('sl_city_id'),
			'district_id' => $this->input->post('sl_district_id'),
			'address' => $this->input->post('tf_address'),
			'geo_location' => $this->input->post('tf_geo_location'),
			'phone' => $this->input->post('tf_phone'),
			'date_begin' => datetime_mysql($this->input->post('tf_date_begin')),
			'date_end' => datetime_mysql($this->input->post('tf_date_end')),
			'quantity' => $this->input->post('tf_quantity'),
		);
		
		$product_image_info =  array('ary_uri_path' => $this->input->post('ckb_uri_path'));
		//end get value from form
		
		$form_info = array_merge($product_detail_info, $product_info,$product_image_info);
		
		if ($this->input->post('btn_submit')) {
			//set form validation
			$form_validation = array(
				array('field' => 'tf_title','label' => 'lang:title','rules' => 'required|max_length[45]'),
				array('field' => 'tf_condition','label' => 'lang:condition','rules' => 'required|max_length[45]'),
				array('field' => 'ta_detail_content','label' => 'lang:detail_content','rules' => 'required|max_length[45]'),
				array('field' => 'tf_price','label' => 'lang:price','rules' => 'required|max_length[45]'),
				array('field' => 'tf_price_discount','label' => 'lang:price_discount','rules' => 'required|max_length[45]'),
				array('field' => 'tf_address','label' => 'lang:address','rules' => 'required|max_length[45]'),
				array('field' => 'sl_country_id','label' => 'lang:country','rules' => 'required|max_length[45]'),
				array('field' => 'sl_city_id','label' => 'lang:city','rules' => 'required|max_length[45]'),
				array('field' => 'sl_district_id','label' => 'lang:district','rules' => 'required|max_length[45]'),
				array('field' => 'tf_phone','label' => 'lang:phone','rules' => 'required|max_length[45]'),
				array('field' => 'tf_date_begin','label' => 'lang:date_begin','rules' => 'required|max_length[45]'),
				array('field' => 'tf_date_end','label' => 'lang:date_end','rules' => 'required|max_length[45]'),
				array('field' => 'tf_quantity','label' => 'lang:quantity','rules' => 'required|max_length[45]'),
			);
			$this->form_validation->set_rules($form_validation);
			
			//--check form validation
			if ($this->form_validation->run()==true) {
				//--update Product table
				$this->Product_model->updateProduct($id,$product_info);
				//--update Product_image table
				$this->Product_image_model->deleteProduct_image($id);
				foreach ($product_image_info['ary_uri_path'] as $key => $value) {
					$ary_info = array(
						'product_id' => $id,
						'uri_path' => $value,
					);
					$this->Product_image_model->addProduct_image($ary_info);
				}				
				//--update Product_detail table
				$this->Product_detail_model->updateProduct_detail($id,$product_detail_info);
				redirect('ads/success');	
			} //end check form validation
		} else { //end post / else
			$form_info = $this->Product_model->getProduct(array('p.id'=>$id));
		}

		$form_info['image'] = $this->Product_image_model->getProduct_image(array('product_id'=>$id));

		$data['city_info'] = $this->City_model->getCities();
		$data['district_info'] = $this->District_model->getDistricts();
		$data['country_info'] = $this->Country_model->getCountries();		
		$data['form_info'] = $form_info;
		$data['inc_script'] = '<script type="text/javascript" src="'.base_url().'js/ads.js"></script>';
		$this->load->view('ads_edit_view',$data);
	}
	
	function success() {
		$data['inc_script'] = '';
		$this->load->view('ads_success_view',$data);
	}
	
	function ajax_upload() {
		//--get field name
		$field = $this->uri->segment(3);
		
		//--upload config
		$config['upload_path'] = BASEPATH."../images/upload/";
		$config['allowed_types'] = 'gif|jpg|png';
		$config['file_name'] = $this->lang->line('site_name') .'-'. $_FILES[$field]['name'];
		$this->load->library('upload',$config);
		
		//--do ajax upload
		echo $this->upload->ajax_do_upload($field);
	}
}